/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
<template>
  <div class="wrap-definition">
    <m-list-construction class="pad-0">
      <template slot="conditions">
        <!-- <div class="chart-title">
          <span>{{$t('Process definition')}}</span>
        </div> -->
        <m-conditions @on-conditions="_onConditions">
          <template slot="button-group">
            <el-button type="primary" size="small" icon="el-icon-plus"  @click="_createDefinition">{{$t('Create process')}}</el-button>
            <el-button type="primary" size="small" plain @click="_uploading">{{$t('Import process')}}</el-button>
          </template>
        </m-conditions>
      </template>
      <template slot="content">
        <template v-if="processListP.length || total>0">
          <m-list :process-list="processListP" @on-update="_onUpdate" :page-no="searchParams.pageNo" :page-size="searchParams.pageSize"></m-list>
          <div class="page-box">
            <el-pagination
              background
              @current-change="_page"
              @size-change="_pageSize"
              :page-size="searchParams.pageSize"
              :current-page.sync="searchParams.pageNo"
              :page-sizes="[10, 30, 50]"
              layout="sizes, prev, pager, next, jumper"
              :total="total">
            </el-pagination>
          </div>
        </template>
        <template v-if="!processListP.length && total<=0">
          <no-data></no-data>
        </template>
        <!-- <m-spin :is-spin="isLoading" :is-left="isLeft"></m-spin> -->
      </template>
    </m-list-construction>
  </div>
</template>
<script>
  import * as _ from 'lodash'
  import { mapActions } from 'vuex'
  import mList from './_source/list'
  import mSpin from '@/view-module/dolphin-scheduler/common/components/spin/spin'
  import localStore from '@/util/dolphin-scheduler/localStorage'
  // import mNoData from '@/view-module/dolphin-scheduler/common/components/noData/noData'
  import listUrlParamHandle from '@/mixins/listUrlParamHandle'
  import mConditions from '@/view-module/dolphin-scheduler/common/components/conditions/conditions'
  import mListConstruction from '@/view-module/dolphin-scheduler/common/components/listConstruction/listConstruction'
  import { findComponentDownward } from '@/util/dolphin-scheduler/'

  export default {
    name: 'definition-list-index',
    data () {
      return {
        total: null,
        processListP: [],
        isLoading: true,
        searchParams: {
          pageSize: 10,
          pageNo: 1,
          searchVal: '',
          userId: ''
        },
        isLeft: true
      }
    },
    mixins: [listUrlParamHandle],
    props: {
    },
    methods: {
      ...mapActions('dag', ['getProcessListP']),
      _createDefinition(){
        this.$breadcrumb.add({
          name: '创建工作流',
          path: ''
        });
        this.$router.push({name: 'definition-create'})
      },
      /**
       * File Upload
       */
      _uploading () {
        findComponentDownward(this.$root, 'roof-nav')._fileUpdate('DEFINITION')
      },
    
      /**
       * conditions
       */
      _onConditions (o) {
        this.searchParams.searchVal = o.searchVal
        this.searchParams.pageNo = 1
        this._debounceGET();
      },
      /**
       * get data list
       */
      _getList (flag) {
        if (sessionStorage.getItem('isLeft') === 0) {
          this.isLeft = false
        } else {
          this.isLeft = true
        }
        this.isLoading = !flag
        this.getProcessListP({
          ...this.searchParams,
          type: '1'
        }).then(res => {
          if (this.searchParams.pageNo > 1 && res.totalList.length === 0) {
            this.searchParams.pageNo = this.searchParams.pageNo - 1
          } else {
            this.processListP = []
            this.processListP = res.totalList
            this.total = res.total
            this.isLoading = false
          }
        }).catch(e => {
          this.isLoading = false
        })
      },
      _onUpdate () {
        this._debounceGET('false')
      },
      _updateList () {
        this.searchParams.pageNo = 1
        this.searchParams.searchVal = ''
        this._debounceGET()
      }
    },
    watch: {
      '$route' (a) {
        // url no params get instance list
        this.searchParams.pageNo = _.isEmpty(a.query) ? 1 : a.query.pageNo
      }
    },
    created () {
      localStore.removeItem('subProcessId')
    },
    mounted () {

    },
    beforeDestroy () {
      sessionStorage.setItem('isLeft', 1)
    },
    components: { mList, mConditions, mSpin, mListConstruction}
  }
</script>

<style lang="scss" rel="stylesheet/scss">

  .wrap-definition {
    .table-box {
      overflow-y: scroll;
    }
    .chart-title {
      // text-align: center;
      // height: 60px;
      // line-height: 60px;
      margin-bottom: 10px;
      span {
        font-size: 18px;
        color: #333;
        font-weight: bold;
      }
    }
    .table-box {
      .fixed {
        table-layout: auto;
        tr {
          th:last-child,td:last-child {
            background: inherit;
            width: 300px;
            height: 40px;
            line-height: 40px;
            border-left:1px solid #ecf3ff;
            position: absolute;
            right: 0;
            z-index: 2;
          }
          td:last-child {
            border-bottom:1px solid #ecf3ff;
          }
          th:nth-last-child(2) {
            padding-right: 330px;
          }
        }
      }
    }
  }
</style>
